import { createRouter, createWebHistory } from 'vue-router';

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/user/login.vue'),
  },
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/user/Home.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: 'creat_patient',
        name: 'CreatePatient',
        component: () => import('../views/patient/CreatPatient.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'patient-case/:patientId',
        name: 'PatientCase',
        component: () => import('../views/patient/PatientCase.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'basic-info/:patientId',
        name: 'PatientBasicInfo',
        component: () => import('../views/patient/PatientBasicInfo.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'epitope-detection-result/:patientId',
        name: 'EpitopeDetectionResult',
        component: () => import('../views/epitope-detection-result/EpitopeDetectionResult.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'vaccine-design/:patientId',
        name: 'VaccineDesign',
        component: () => import('../views/vaccine-result/VaccineDesign.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'clinical-observation/:patientId',
        name: 'ClinicalObservation',
        component: () => import('../views/clinical-observation/ClinicalObservation.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'editor',
        name: 'EditorArticle',
        component: () => import('../components/WangEditor.vue'),
        meta: { requiresAuth: true }
      },
      
    ],
    
  },
  
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 模拟一个简单的用户认证状态
let isAuthenticated = true;

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!isAuthenticated) {
      next({
        path: '/login',
        query: { redirect: to.fullPath },
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
